`
https://leetcode.cn/problems/heaters/
`

/**
 * @param {number[]} houses
 * @param {number[]} heaters
 * @return {number}
 */
var findRadius = function (houses, heaters) {
  let res = 0
  heaters.sort((a, b) => a - b)

  for (const h of houses) {
    // 找到 h 左边最近的供暖
    // 最后一个 < h
    const l = _.sortedIndex(heaters, h) - 1
    // h 右边最近的供暖
    const r = l + 1
    // 距离左右边最近供暖距离
    const leftDistance = l < 0 ? Infinity : h - heaters[l]
    const rightDistance = r >= heaters.length ? Infinity : heaters[r] - h
    // 最近即可供暖的距离
    const curDistance = Math.min(leftDistance, rightDistance)
    // 更新答案
    res = Math.max(res, curDistance)
  }

  return res
};